var MyAccountCtrl = ['$scope', '$rootScope', function ($scope, $rootScope) {
        $scope.menu_list = [
            {name: $rootScope.language.TIPS_PERSON_ALDETIALS, url: "#/myaccount/personal_info", id: "personal_info", type: 2, show: $rootScope.purview['\/myaccount\/personal_info'].show},
            {name: $rootScope.language.TIPS_CHANGE_PASSWORD, url: "#/myaccount/change_pass", id: "change_pass", type: 2, show: $rootScope.purview['\/myaccount\/change_pass'].show},
            {name: $rootScope.language.TIPS_ATTENDANCE_UPLOAD, url: "#/myaccount/attend_upload", id: "attend_upload", type: 2, show: $rootScope.purview['\/myaccount\/attend_upload'].show},
            {name: $rootScope.language.TIPS_MY_PERFORMANCE, url: "#/myaccount/performance_select", id: "performance_select", type: 2, show: $rootScope.purview['\/myaccount\/performance_select'].show},
            {name: $rootScope.language.TIP_MYACCOUNT_PERSON_SETTLE, url: "#myaccount/scheduling", id: "scheduling", type: 2, show: $rootScope.purview['\/myaccount\/scheduling'].show}
        ];
    }];

var PersonalInfoCtrl = ['$scope', '$rootScope', '$location', 'UserLoginCheck', 'UserSetPicture', 'PosUser', function ($scope, $rootScope, $location, UserLoginCheck, UserSetPicture, PosUser) {
        $scope.curr = 'personal_info';

        //权限检查
        $rootScope.purviewCheckChange('\/myaccount', '/index');
        //获取当前收银员头像
        var user_head = $api.getStorage('user_head') ? $api.getStorage('user_head') : {};
        if (typeof $rootScope.user === "undefined" || typeof user_head[$rootScope.user.UserCode] === "undefined") {
            $scope.user_img = "./image/head_img_default.png";
        } else {
            $scope.user_img = user_head[$rootScope.user.UserCode];
        }
        //收银员头像设置
        $scope.setPicture = function () {
            //权限检查
            if ($rootScope.purviewCheckChange('\/myaccount\/personal_info:head')) {
                return;
            }

            UserSetPicture($rootScope.user.UserCode).then(function (url) {
                $scope.user_img = url;
            });
        };


        //用户信息
        $scope.personInfo = {
            PersonTel: $rootScope.user.UserTel,
            PersonName: $rootScope.user.UserName,
            PersonCode: $rootScope.user.UserCode,
            PersonManage: $rootScope.user.UserManage
        };

        //用户信息编辑
        $scope.personInfo.test = 0;
        $scope.submitEdit = function (personInfo) {
            if ($rootScope.user.UserCode == 000) {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_MYACCOUNT_PERSON_PASSWORD});
                return;
            }
            if ($scope.personInfo.test) {

                //权限检查
                if ($rootScope.purviewCheckChange('\/myaccount\/personal_info:info')) {
                    return;
                }

                if (typeof personInfo.PersonName == 'undefined') {
                    baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.MSG_NAME_REQUIRED});
                    return;
                } else if (typeof personInfo.PersonTel == 'undefined') {
                    baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.MSG_TEL_INPUT_ERROR});
                    return;
                }
                var data = {
                    UserTel: personInfo.PersonTel,
                    UserStatus: '1',
                    UserName: personInfo.PersonName
                };
                PosUser.save_user_by_code(personInfo.PersonCode, data).then(function (success) {
                    baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language[success.data.code]});
                    if (success.data.status == '1') {
                        $rootScope.user.UserTel = $scope.personInfo.PersonTel;
                        $rootScope.user.UserName = $scope.personInfo.PersonName;
                    } else {
//        $scope.personInfo = $rootScope.user;
                        $scope.personInfo = {
                            PersonTel: $rootScope.user.UserTel,
                            PersonName: $rootScope.user.UserName,
                            PersonCode: $rootScope.user.UserCode
                        };
                    }
                });
            }
            $scope.personInfo.test = !$scope.personInfo.test;
//    };

        };
    }];

var ChangePassCtrl = ['$scope', '$rootScope', '$location', 'UserLoginCheck', 'UserSetPicture', 'PosUser', '$window', function ($scope, $rootScope, $location, UserLoginCheck, UserSetPicture, PosUser, $window) {
        $scope.changePass = '';
        if ($rootScope.user.UserCode == '000') {
            $window.history.back();
            baison_app.api.toast({msg: $rootScope.language.TIP_MYACCOUNT_PERSON_PASSWORD});
            return;
        }
        $scope.curr = 'change_pass';

        //修改登陆密码
        $scope.submitNewPass = function () {
            if (typeof $scope.changePass.oldPass == 'undefined') {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_MYACCOUNT_PASSWORD_OLD});
            } else if ($.md5($scope.changePass.oldPass) != $rootScope.user.UserPass) {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_MYACCOUNT_PASSWORD_OLD});
            } else if ($scope.changePass.newPass1 == '' || typeof $scope.changePass.newPass1 == 'undefined' || $scope.changePass.newPass2 == '' || typeof $scope.changePass.newPass2 == 'undefined') {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_MYACCOUNT_PASSWORD_OLD_CHECK})
            } else if ($scope.changePass.newPass1 != $scope.changePass.newPass2) {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_MYACCOUNT_PERSON_PASSWORD_CHECK});
            } else {
                var data = {};
//                $rootScope.user.UserPass = $.md5($scope.changePass.newPass1);
                data.UserCode = $rootScope.user.UserCode;
                data.UserPass = $scope.changePass.newPass1;
                PosUser.save_user_by_code($rootScope.user.UserCode, data, data.UserPass).then(function (success) {
                    if (success.data.status == '1') {
                        baison_app.api.toast({msg: $rootScope.language.TIP_MYACCOUNT_PERSON_PASSWORD_ALERT});
                        $rootScope.user = {};
                        $location.path('/login');
                    } else {
                        baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language[success.data.code]});
                    }
                });
            }
        };

        //修改签名码
        $scope.submitNewSignPass = function () {
            PosUser.get_sign_by_code($rootScope.user.UserCode).then(function (ret) {
                $scope.changePass.SignPass = ret[0].signatureCode;

                console.log($scope.changePass.SignPass)

                if ($scope.changePass.SignPass == '') {
                    $scope.changePass.SignPass = $rootScope.user.UserPass;
                }
                if (typeof $scope.changePass.oldSignPass == 'undefined') {
                    baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_MYACCOUNT_PASSWORD_OLD});
                } else if ($.md5($scope.changePass.oldSignPass) != $scope.changePass.SignPass) {
                    baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_MYACCOUNT_PASSWORD_OLD});
                } else if ($scope.changePass.newSignPass1 == '' || typeof $scope.changePass.newSignPass1 == 'undefined' || $scope.changePass.newSignPass2 == '' || typeof $scope.changePass.newSignPass2 == 'undefined') {
                    baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_MYACCOUNT_PASSWORD_OLD_CHECK})
                } else if ($scope.changePass.newSignPass1 != $scope.changePass.newSignPass2) {
                    baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_MYACCOUNT_PERSON_PASSWORD_CHECK});
                } else {
                    PosUser.change_sign_pass($scope.changePass.newSignPass1).then(function (success) {
                        baison_app.api.alert({msg: 'SUCCESS'});
                    }).catch(function (err) {
                        baison_app.api.alert({msg: err});
                    });
                }
            });

        };
        //二者切换
        $scope.show_curr = 'pwd0';
        $scope.tab_li = function (n) {
            $scope.show_curr = 'pwd' + n;
        };
    }];

/**
 * 考勤数据上传
 * @author jhua.zuo<jhua.zuo@baisonmail.com>
 * @date 2015-12-14
 */
var AttendUploadCtrl = ['$rootScope', '$scope', '$route', 'Upload', 'ShopAttend', 'PosUser', function ($rootScope, $scope, $route, Upload, ShopAttend, PosUser) {
        $scope.curr = 'attend_upload';
        //待上传的文件数据
        $scope.updata = {
            date: null,
            file: null,
            file_name: ''
        };
        //已经上传的文件列表，通过接口获得 ------------------------------------------
        $scope.list = {};
        ShopAttend.get_list().then(function (response) {
            if (response.data.status == 1) {
                $scope.list = response.data.data.data;
            }
        });

        //选择待上传的文件 --------------------------------------------------------
        $scope.file_select = function (file) {
            if (file != undefined) {
                $scope.updata['file'] = file;
                $scope.updata.file_name = file.name;
            }

        };

        //确认上传 ---------------------------------------------------------------
        $scope.upload_test = 0;
        $scope.file_upload = function () {
            if ($scope.upload_test == 1) {
                baison_app.api.toast({msg: $rootScope.language.TIP_MYACCOUNT_FILE_UPLOAD});
                return;
            }

            if ($scope.updata['date'] == null) {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_MYACCOUNT_FILE_UPLOAD_DATE});
                return;
            }

            if ($scope.updata['file_name'] == '') {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_MYACCOUNT_FILE_UPLOAD_FILE});
                return;
            }

            $scope.upload_test = 1;

            ShopAttend.upload_data($scope.updata['file'], $scope.updata['date']).then(function (response) {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIPS_CUNHUO_CONFIRM_SUCCESS});
                $route.reload();
            }, function (err) {
                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], title: $rootScope.language.COMMON_TITLE_HINT, msg: err});
            }, function (evt) {
                //进度百分比，暂时不用
                var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                baison_app.api.toast({msg: progressPercentage + '%'});
                if (progressPercentage == '100') {
                    $scope.upload_test = 1;
                }
            });
        };

        //重置上传信息 -----------------------------------------------------------
        $scope.reset_upload = function () {
            $scope.updata = {
                date: null,
                file: null,
                file_name: ''
            };
        };

    }];

//业绩晴雨表
var PerformanceSelectCtrl = ['$scope', '$rootScope', '$location', 'PeFormanceSelect', 'ngDialog', 'PosUser', 'timeStamp2String', '$sce', '$compile', function ($scope, $rootScope, $location, PeFormanceSelect, ngDialog, PosUser, timeStamp2String, $sce, $compile) {
        var window_id = 0;
        var now_date = new Date();
        $scope.now_date = new Date();
        $scope.curr = 'performance_select';
        $scope.cur = {
            date: null
        };
        var regex = /^(([1-9]\d*)|0)(\.\d{2})?$/;
        PosUser.get_user_by_code($rootScope.user.UserCode).then(function (res) {
            if (res.role_id == 13) {    //13是店长，14是店员
                $scope.dianzhang = 1;
                $scope.dianyuan = 0;
            } else {
                $scope.dianzhang = 0;
                $scope.dianyuan = 1;
            }

        });
        //关闭
        $scope.close = function () {
            $location.path('/myaccount/performance_select');
        };

        //搜索某月指标情况
        $scope.search = function () {
            $scope.search_shop_month = {};
            if ($scope.cur['date'] == null) {
                var _date = new Date();
                _date.setDate(1);
                $scope.cur.date = _date;              
                //baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON],title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_MYACCOUNT_SEARCH_MONTH});
                //return;
            }
            //选择的月份与当月月份进行比较。若小，则不允许设置
//          if(($scope.cur.date.getMonth() + 1) < ($scope.now_date.getMonth() + 1)){
//          	$scope.hideOperation = 0;
//          }else{
//          	$scope.hideOperation = 1;
//          }
            
            var start_date = timeStamp2String((get_first_day($scope.cur.date)), 'date');
            var end_date = timeStamp2String((get_first_day($scope.cur.date) + 6 * 86400000 + (4 - 1) * 7 * 86400000), 'date');
            //PeFormanceSelect.get_list(date);
     
            $scope.month = $scope.cur.date.getMonth() + 1;
                               
            PeFormanceSelect.get_shop_month_indicators($scope.cur.date).then(function (response) {
                if (response.data.status == 1) {
                    $.each(response.data.data.data, function (i, n) {
                        $scope.search_shop_month = {
                            indicators: n.indicators,
                            actual: n.actual,
                            rate: n.rate,
                            unfinished: (n.indicators - n.actual) >= 0 ? (n.indicators - n.actual) : 0
                        };
                        //根据搜索月份刷新月指标
//                      $scope.shop_month = {
//                          indicators: n.indicators,
//                          actual: n.actual,
//                          rate: n.rate,
//                          unfinished: (n.indicators - n.actual) >= 0 ? (n.indicators - n.actual) : 0
//                      };
//                      $scope.month_adjust_indicators = $scope.shop_month.indicators;
                    });
//              }else {
//                  $scope.shop_month = {
//                      indicators: 0,
//                      actual: 0,
//                      rate: '0%',
//                      unfinished: 0
//                  };
//                  $scope.month_adjust_indicators = $scope.shop_month.indicators;
                }
            });

            $scope.search_shop_week = [];
            PeFormanceSelect.get_shop_week_indicators(start_date, end_date).then(function (response) {
                if (response.data.status == 1) {
                    $.each(response.data.data.data, function (i, n) {
                        $scope.search_shop_week.push({
                            indicators: n.indicators,
                            actual: n.actual,
                            rate: n.rate,
                            unfinished: (n.indicators - n.actual) >= 0 ? (n.indicators - n.actual) : 0
                        });
                        //根据搜索月份刷新周指标
//                       $scope.shop_week = {
//                          indicators: n.indicators,
//                          actual: n.actual,
//                          rate: n.rate,
//                          unfinished: (n.indicators - n.actual) >= 0 ? (n.indicators - n.actual) : 0
//                      };
//                      $scope.week_indicators = $scope.shop_week.indicators;
                    });
//              }else {
//                  $scope.shop_week = {
//                      indicators: 0,
//                      actual: 0,
//                      rate: '0%',
//                      unfinished: 0
//                  };
//                  $scope.week_indicators = $scope.shop_week.indicators;
                }
            });
            
            //根据搜索月份刷新导购的业绩排名
            $scope.dianyuan_month_indicators = [];
            PosUser.get_user_by_shopCode($rootScope.current_shop.ShopCode).then(function (user) {
	            user.forEach(function (data, index) {
	                $scope.dianyuan_month_indicators.push({user_name: data.UserName, code: data.UserCode, weight: '1.00', month_indicators: '0.00', actual: 0, rate: '0.00%', unfinished: '0'})
	            });
	            //获取导购员月指标
	            _get_dianyuan_month_indicators($scope.cur.date);
	        });

        };
        
        $scope.search();
        
        //获取当月指标
        var _get_shop_month_indicators = function (date) {
            PeFormanceSelect.get_shop_month_indicators(date).then(function (response) {
                if (response.data.status == 1) {
                    $.each(response.data.data.data, function (i, n) {
                        $scope.shop_month = {
                            indicators: n.indicators,
                            actual: n.actual,
                            rate: n.rate,
                            unfinished: (n.indicators - n.actual) >= 0 ? (n.indicators - n.actual) : 0
                        };
                        $scope.month_adjust_indicators = $scope.shop_month.indicators;
                    });
                } else {
                    $scope.shop_month = {
                        indicators: 0,
                        actual: 0,
                        rate: '0%',
                        unfinished: 0
                    };
                }
            });
        };
        _get_shop_month_indicators($scope.now_date);
        //获取本周指标
        var _get_shop_week_indicators = function (date) {
            PeFormanceSelect.get_shop_week_indicators(date).then(function (response) {
                if (response.data.status == 1) {
                    $.each(response.data.data.data, function (i, n) {
                        $scope.shop_week = {
                            indicators: n.indicators,
                            actual: n.actual,
                            rate: n.rate,
                            unfinished: (n.indicators - n.actual) >= 0 ? (n.indicators - n.actual) : 0
                        };
                        $scope.week_indicators = $scope.shop_week.indicators;
                    });
                } else {
                    $scope.shop_week = {
                        indicators: 0,
                        actual: 0,
                        rate: '0%',
                        unfinished: 0
                    };
                }
            });
        };
        _get_shop_week_indicators($scope.now_date);
        //获取当日指标
        PeFormanceSelect.get_shop_day_indicators($scope.now_date).then(function (response) {
            //baison_app.api.alert({title:'rizhib',msg:response});
            if (response.data.status == 1) {
                $.each(response.data.data.data, function (i, n) {
                    $scope.shop_day = {
                        indicators: n.indicators,
                        actual: n.actual,
                        rate: n.rate,
                        unfinished: (n.indicators - n.actual) >= 0 ? (n.indicators - n.actual) : 0
                    };
                });
            } else {
                $scope.shop_day = {
                    indicators: 0,
                    actual: 0,
                    rate: '0%',
                    unfinished: 0
                };
            }
        });
        
        
        
        
        //获取店铺一周每天权重
        var _get_shop_day_weight = function (date) {
            PeFormanceSelect.get_shop_day_weight(date).then(function (response) {
                //baison_app.api.alert({title:'wei',msg:response});
                if (response.data.status == 1) {
                    var n = response.data.data;
                    $scope.weight = {
                        sun: n.sun,
                        mon: n.mon,
                        tue: n.tue,
                        wed: n.wed,
                        thu: n.thu,
                        fri: n.fri,
                        sat: n.sat
                    };

                }
            });
        };
        _get_shop_day_weight($scope.now_date);
        //设置成功
        var set_success = function () {
            ngDialog.close(window_id);
            baison_app.api.toast({msg: $rootScope.language.TIP_INDEX_SET_SUCCESS});
        };
        //设置权重
        $scope.update_shop_day_weight = function (weight) {
            var flag = true;
            for (var e in weight) {
                if (!regex.test(weight[e])) {
                    baison_app.api.toast({msg: $rootScope.language.TIPS_CET_RIGHT_NUM});
                    return flag = false;
                }
            }
            if (flag) {
                PeFormanceSelect.update_shop_day_weight(weight, $scope.date).then(function (res) {
                    if (res.data.status == 1) {
                        _get_shop_day_weight($scope.now_date);
                        set_success();
                    }
                });
            }
        };


        //店铺当月指标设置
        $scope.update_shop_month_indicators = function (indicators) {
            if (!regex.test(indicators)) {
                baison_app.api.toast({msg: $rootScope.language.TIPS_CET_RIGHT_NUM});
                return;
            }
            //,$scope.cur.date
            PeFormanceSelect.update_shop_month_indicators(indicators).then(function (res) {
                if (res.data.status == 1) {
                    _get_shop_month_indicators($scope.now_date);
                    set_success();
                }
            });
        };
        //店铺本周指标设置
        $scope.update_shop_week_indicators = function (indicators) {
            if (!regex.test(indicators)) {
                baison_app.api.toast({msg: $rootScope.language.TIPS_CET_RIGHT_NUM});
                return;
            }
            //,$scope.cur.date
            PeFormanceSelect.update_shop_week_indicators(indicators).then(function (res) {
                //baison_app.api.alert({title:"update_shop_week_indicators",msg:res});
                if (res.data.status == 1) {
                    _get_shop_week_indicators($scope.now_date);
                    set_success();
                }
            });
        };
        //获取某个日期当月的第一个星期的星期一
        function get_first_day(date) {
            var now = new Date();
            var year = now.getFullYear();
            var month = now.getMonth();   //(0~11)
            var week = now.getDay();       //(0~11)
            if (week == 0) {
                week = 7
            }
            var day = now.getDate();
            //var date = new Date();
            date.setDate(1);
            var mn_start = date.getTime();
            var weekday_mnstart = date.getDay();
            if (weekday_mnstart == 0) {
                weekday_mnstart = 7
            }
            if (weekday_mnstart < 5) {
                var differ = (weekday_mnstart - 1) * 86400000;
                var start_day = mn_start - differ;
            } else {
                var differ = (8 - weekday_mnstart) * 86400000;
                var start_day = mn_start + differ;
            }
            //var first_day = new Date(start_day).toLocaleString();    //时间戳传字符串
            return start_day;
        }
        $scope.every_week_indicators = [];
        for (var i = 1; i < 5; i++) {
            $scope.every_week_indicators.push(
                    {
                        week: i,
                        start_date: timeStamp2String((get_first_day(now_date) + (i - 1) * 7 * 86400000), 'date'),
                        end_date: timeStamp2String((get_first_day(now_date) + 6 * 86400000 + (i - 1) * 7 * 86400000), 'date'),
                        indicators: ''
                    }
            );
        }
        //获取每周周指标
        var _get_shop_every_week_indicators = function (date1, date2) {
            PeFormanceSelect.get_shop_week_indicators(date1, date2).then(function (response) {
                if (response.data.status == 1) {
                    $.each(response.data.data.data, function (i, n) {
                        $scope.every_week_indicators[n.week - 1].indicators = n.indicators;
                    });
                    $scope._every_week_indicators = $scope.every_week_indicators;
                }
            });
        };
        _get_shop_every_week_indicators($scope.every_week_indicators[0].start_date, $scope.every_week_indicators[3].end_date);
        //店铺每周指标设置
        $scope.update_shop_every_week_indicators = function () {
            var flag = true;
            $scope.every_week_indicators.forEach(function (e, i) {
                if (!regex.test(e.indicators)) {
                    baison_app.api.toast({msg: $rootScope.language.TIPS_CET_RIGHT_NUM});
                    return flag = false;
                }
            });
            if (flag) {
                var week = $scope.every_week_indicators;
                var indicators = '';   //111_1_2016-02-29_2016-03-06,222_2_2016-03-07_2016-03-13,333_3_2016-03-14_2016-03-20,444_4_2016-03-21_2016-03-27
                for (var i = 0; i < 4; i++) {
                    if (week[i].indicators == '') {
                        baison_app.api.toast({msg: "请输入有效的指标"});
                        return;
                    }
                    indicators = indicators + week[i].indicators + '_' + week[i].week + '_' + week[i].start_date + '_' + week[i].end_date + ','
                }
                indicators = indicators.substring(0, indicators.length - 1);
                PeFormanceSelect.update_shop_every_week_indicators(indicators).then(function (res) {
                    if (res.data.status == 1) {
                        set_success();
                    }
                    //baison_app.api.alert({title:"update_shop_every_week_indicators",msg:res});
                });
            }
        };

        //分解月指标指标
        $scope.fenjie_month_indicators = function () {
            if ($scope.shop_month.indicators != '' && $scope.shop_month.indicators != 0) {
                var indicators = $scope.shop_month.indicators;
            } else {
                baison_app.api.alert({msg: "请先设置月指标"});
                return;
            }
            for (var i = 0; i < 4; i++) {
                $scope.every_week_indicators[i].indicators = indicators / 4;
            }
        };
//获取店铺每日指标
        var shop_day_indicators = [];
        PeFormanceSelect.get_all_day($scope.now_date).then(function (response) {
            //baison_app.api.alert({title:'rizhib',msg:response});
            if (response.data.status == 1) {
                shop_day_indicators = response.data.data.data;
                var now = new Date();
                $scope.date = now.getFullYear() + '-' + (now.getMonth() + 1);
                $scope.calendar = calendar(shop_day_indicators, now);
                //baison_app.api.alert({title:'1555',msg:shop_day_indicators});
            }
        });
        //TODO 店铺日指标设置
        var now = new Date();
        $scope.date = now.getFullYear() + '-' + (now.getMonth() + 1);
        $scope.calendar = calendar(shop_day_indicators, now);
        //var text = angular.element($scope.calendar);
        //var content = $compile(text)($scope);
        //baison_app.api.alert({msg:shop_day_indicators});
        //上一个月、下一个月
        $scope.set_month = function (i) {
            var date = $scope.date.split("-");
            var new_date = new Date(date[0], date[1] - 1 + i, 1);
            $scope.date = new_date.getFullYear() + '-' + (new_date.getMonth() + 1);
            $scope.calendar = calendar(shop_day_indicators, new_date);
            PeFormanceSelect.get_all_day(new_date).then(function (response) {
                if (response.data.status == 1) {
                    shop_day_indicators = response.data.data.data;
                    $scope.date = new_date.getFullYear() + '-' + (new_date.getMonth() + 1);
                    $scope.calendar = calendar(shop_day_indicators, new_date);
                }
            });
            _get_shop_month_indicators(new_date);
        };
        $scope.date_month = $scope.now_date;
        $scope.search_month = function (date) {
            $scope.date = date.getFullYear() + '-' + (date.getMonth() + 1);
            PeFormanceSelect.get_all_day(date).then(function (response) {
                if (response.data.status == 1) {
                    shop_day_indicators = response.data.data.data;
                    $scope.calendar = calendar(shop_day_indicators, date);
                }
            });
            _get_shop_month_indicators(date);
        };

        //店铺月指标分摊到店铺每日
        $scope.fentan = function () {
            baison_app.api.confirm({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON, $rootScope.language.ACT_CANCEL], title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.TIP_MYACCOUNT_SHOP_MONTH_CONFIRM}, function (ret) {
                if (ret.buttonIndex == 1) {
                    var day_weight = []; //每天的权重
                    day_weight[0] = 0;
                    var str = $scope.date.split('-');
                    var year = str[0];
                    var month = str[1];
                    var date = new Date(year, month - 1, 1);
                    var last_day = new Date(year, month, 0);
                    var day_num = last_day.getDate();
                    var count_weight = 0;
                    for (var i = 0; i <= day_num - 1; i++) {
                        var day = new Date(year, month - 1, i + 1).getDay();
                        //baison_app.api.alert({title:'weekday',msg:i+':'+day});
                        switch (day) {
                            case 0:
                                day_weight[i] = $scope.weight['sun'];
                                break;
                            case 1:
                                day_weight[i] = $scope.weight['mon'];
                                break;
                            case 2:
                                day_weight[i] = $scope.weight['tue'];
                                break;
                            case 3:
                                day_weight[i] = $scope.weight['wed'];
                                break;
                            case 4:
                                day_weight[i] = $scope.weight['thu'];
                                break;
                            case 5:
                                day_weight[i] = $scope.weight['fri'];
                                break;
                            case 6:
                                day_weight[i] = $scope.weight['sat'];
                                break;
                        }
                        count_weight += parseFloat(day_weight[i]);
                    }
                    //baison_app.api.alert({title:'weekday',msg:day_weight});
                    PeFormanceSelect.fentan(day_weight, date, count_weight).then(function (res) {
                        //TODO
                        //baison_app.api.alert({title:'fentan',msg:res});
                        PeFormanceSelect.get_all_day($scope.now_date).then(function (response) {
                            //baison_app.api.alert({title:'rizhib',msg:response});
                            if (response.data.status == 1) {
                                shop_day_indicators = response.data.data.data;
                                var now = new Date();
                                $scope.date = now.getFullYear() + '-' + (now.getMonth() + 1);
                                $scope.calendar = calendar(shop_day_indicators, now);
                                //baison_app.api.alert({title:'1555',msg:shop_day_indicators});
                            }
                        });
                    })
                }
            });
        };





        //店员日指标设置1>>>>>>>>>
        $scope.dianyuan_day_indicators = [];
        var _get_dianyuan_day_indicators = function (date) {
            PeFormanceSelect.get_dianyuan_day_indicators(date).then(function (res) {
                if (res.data.status == 1) {
                    $.each(res.data.data.data, function (i, n) {
                        $scope.dianyuan_day_indicators.forEach(function (data, index) {
                            if (n.dydm == data.code) {
                                data.weight = n.zbqz;
                                data.day_indicators = n.zbje;
                            }
                        });

                    });
                }

            });
        };
        PosUser.get_user_by_shopCode($rootScope.current_shop.ShopCode).then(function (user) {
            user.forEach(function (data, index) {
                $scope.dianyuan_day_indicators.push({user_name: data.UserName, code: data.UserCode, weight: '1.00', day_indicators: 0})
            });
            $scope.checked_all(true);

            //获取导购员日指标
            _get_dianyuan_day_indicators($scope.now_date);
        });
        $scope.shop_day_to_dianyuan = function () {
            $scope.day_weight_count = 0;
            $scope.day_indicators_count = 0;
            for (var i = 0; i < $scope.dianyuan_day_indicators.length; i++) {
                if ($scope.dianyuan_day_indicators[i].checked) {
                    $scope.day_weight_count += parseFloat($scope.dianyuan_day_indicators[i].weight);
                }
            }
            var avg_indicators = $scope.shop_day.indicators / $scope.day_weight_count;
            $scope.dianyuan_day_indicators.forEach(function (e, i) {
                if ($scope.dianyuan_day_indicators[i].checked) {
                    e.day_indicators = (avg_indicators * parseFloat($scope.dianyuan_day_indicators[i].weight)).toFixed(0);
                } else {
                    e.day_indicators = 0;
                }
            });
            for (var n = 0; n < $scope.dianyuan_day_indicators.length; n++) {
                $scope.day_indicators_count += parseFloat($scope.dianyuan_day_indicators[n].day_indicators);
            }
            var index = -1;
            $scope.dianyuan_day_indicators.forEach(function (e, i) {
                if ($scope.dianyuan_day_indicators[i].checked) {
                    index = i;
                }
            });
            if (index != -1) {
                $scope.dianyuan_day_indicators[index].day_indicators = parseFloat($scope.dianyuan_day_indicators[index].day_indicators) + parseFloat($scope.shop_day.indicators - $scope.day_indicators_count);
            }
            $scope.day_indicators_count = $scope.shop_day.indicators;
        };
        //保存店日指标分摊到店员
        $scope.save_dianyuan_day = function () {
            var flag = true;
            $scope.dianyuan_day_indicators.forEach(function (e, i) {
                if (!regex.test(e.day_indicators) || !regex.test(e.weight)) {
                    baison_app.api.toast({msg: $rootScope.language.TIPS_CET_RIGHT_NUM});
                    return flag = false;
                }
            });
            if (flag) {
                PeFormanceSelect.shop_day_to_dianyuan($scope.now_date, $scope.dianyuan_day_indicators).then(function (res) {
                    //baison_app.api.alert({msg:res});
                    _get_dianyuan_day_indicators($scope.now_date);
                    set_success();
                });
            }
        };
        $scope.checked_all = function (select_all) {
            for (var i = 0; i < $scope.dianyuan_day_indicators.length; i++) {
                $scope.dianyuan_day_indicators[i].checked = select_all;
            }
        };
        //<<<<<<<<<<<<<<<<<<<<1

        //>>>>>>>>>>>>>>>>>>>
        $scope.dianyuan_month_indicators = [];
        var _get_dianyuan_month_indicators = function (date) {
            PeFormanceSelect.get_dianyuan_month_indicators(date).then(function (res) {
                if (res.data.status == 1) {
                    $.each(res.data.data.data, function (i, n) {
                        $scope.dianyuan_month_indicators.forEach(function (data, index) {
                            if (n.dydm == data.code) {
                                data.month_indicators = n.zbje;
                                data.actual = parseFloat(n.xsje);
                                data.rate = n.zbje > 0 ? ((n.xsje / n.zbje) * 100).toFixed(2) + '%' : '0.00%';
                                data.unfinished = (n.zbje - n.xsje) >= 0 ? (n.zbje - n.xsje) : 0;
                                data.weight = n.qz
                            }
                        });

                    });
                }

            });
        };
        PosUser.get_user_by_shopCode($rootScope.current_shop.ShopCode).then(function (user) {
            user.forEach(function (data, index) {
                $scope.dianyuan_month_indicators.push({user_name: data.UserName, code: data.UserCode, weight: '1.00', month_indicators: '0.00', actual: 0, rate: '0.00%', unfinished: '0'})
            });
            //获取导购员月指标
            _get_dianyuan_month_indicators($scope.now_date);
        });


        //店铺月指标分配到店员
        $scope.shop_month_to_dianyuan = function () {
            $scope.weight_count = 0;
            $scope.indicators_count = 0;
            for (var i = 0; i < $scope.dianyuan_month_indicators.length; i++) {
                $scope.weight_count += parseFloat($scope.dianyuan_month_indicators[i].weight);
            }
            var avg_indicators = $scope.shop_month.indicators / $scope.weight_count;
            $scope.dianyuan_month_indicators.forEach(function (e, i) {
                e.month_indicators = (avg_indicators * parseFloat($scope.dianyuan_month_indicators[i].weight)).toFixed(0);
            });
            for (var n = 0; n < $scope.dianyuan_month_indicators.length; n++) {
                $scope.indicators_count += parseFloat($scope.dianyuan_month_indicators[n].month_indicators);
            }
        };
        //
        $scope.save_dianyuan_month = function () {
            var flag = true;
            $scope.dianyuan_month_indicators.forEach(function (e, i) {
                if (!regex.test(e.month_indicators) || !regex.test(e.weight)) {
                    baison_app.api.toast({msg: $rootScope.language.TIPS_CET_RIGHT_NUM});
                    return flag = false;
                }
            });
            if (flag) {
                PeFormanceSelect.save_shop_month_to_dianyuan($scope.now_date, $scope.dianyuan_month_indicators).then(function (res) {
                    //baison_app.api.alert({msg:res});
                    _get_dianyuan_month_indicators($scope.now_date);
                    set_success();
                });
            }
        };
        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        //获取当前店员一个月的所有日指标
        $scope.get_dianyuan_every_day_indicators = function () {
            $scope.dianyuan_every_day_indicators = [];
            var date = $scope.cur.date;
            var year = date.getFullYear();
            var month = date.getMonth();
            var first_day = new Date(year, month, 1);
            var last_day = new Date(year, month + 1, 0);
            var user_code = $rootScope.user.UserCode;
            PeFormanceSelect.get_dianyuan_day_indicators(first_day, last_day, user_code).then(function (res) {
                if (res.data.status == 1) {
                    if(res.data.data.data.length > 0){
	                    	$.each(res.data.data.data, function (i, n) {
	                        var rate = '0%';
	                        if (parseFloat(n.zbje) != 0) {
	                            rate = ((parseFloat(n.xsje) / parseFloat(n.zbje)) * 100).toFixed(2) + '%';
	                        }
	                        $scope.dianyuan_every_day_indicators.push({
	                            date: timeStamp2String(n.rq * 1000, 'date'),
	                            dainyuan: $rootScope.user.UserName,
	                            indicators: n.zbje,
	                            actual: n.xsje,
	                            weight: n.zbqz,
	                            rate: rate,
	                            unfinished: (n.zbje - n.xsje) >= 0 ? (n.zbje - n.xsje) : 0
	                        });
	                    });
                    }else{
                       //若没有业绩的话
                    	$scope.dianyuan_every_day_indicators.push({
	                            date: timeStamp2String($scope.cur.date, 'date'),
	                            dainyuan: $rootScope.user.UserName,
	                            indicators: 0.00,
	                            actual: 0.00,
	                            weight: 0.00,
	                            rate: '0%',
	                            unfinished: 0.00
	                        });
                    }       
                }
            });
        };
        $scope.get_dianyuan_every_day_indicators();
        
        $scope._get_dianyuan_month_indicators = function () {
            var user_code = $rootScope.user.UserCode;
            PeFormanceSelect.get_dianyuan_month_indicators($scope.cur.date, user_code).then(function (res) {
                $scope.dianyuan_month = {};
                var rate = '0%';
                if (res.data.status == 1) {                  
                    var data = res.data.data.data[0];
                    if(data.length>0){
                    	if (parseFloat(data.zbje) != 0) {
	                        rate = ((parseFloat(data.xsje) / parseFloat(data.zbje)) * 100).toFixed(2) + '%';
	                    }
	                    $scope.dianyuan_month = {
	                        dianyuan: $rootScope.user.UserName,
	                        weight: data.xsje,
	                        indicators: data.zbje,
	                        actual: data.xsje,
	                        rate: rate,
	                        unfinished: (data.zbje - data.xsje) >= 0 ? (data.zbje - data.xsje) : 0
	                    }
                    }else{
                    	//没有业绩的话
                    	$scope.dianyuan_month = {
	                        dianyuan: $rootScope.user.UserName,
	                        weight: 0.00,
	                        indicators: 0.00,
	                        actual: 0.00,
	                        rate: '0%',
	                        unfinished: 0.00
	                    }
                    }
                    
                }
            });
        };
        $scope._get_dianyuan_month_indicators();

        $scope.set_dialog = function (dialog) {
            window_id = ngDialog.open({closeByDocument: false,
                trapFocus: false,
                template: './html/myaccount/dialog/' + dialog + '.html',
                disableAnimation: true,
                scope: $scope,
                preCloseCallback: function () {
                    switch (dialog) {
                        case 'shop_every_week_set':
                            _get_shop_every_week_indicators($scope.every_week_indicators[0].start_date, $scope.every_week_indicators[3].end_date);
                            break;
                        case 'dianyuan_month_set':
                            _get_dianyuan_month_indicators($scope.now_date);
                            break;
                        case 'dianyuan_day_set':
                            _get_dianyuan_day_indicators($scope.now_date);
                            break;
                        case 'weight_set':
                            _get_shop_day_weight($scope.now_date);
                            break;
                    }
                }
            });
        };
        $scope.cancel = function () {
            ngDialog.close(window_id);
        };
    }];


//人员排班
var ScheduleCtrl = ['$scope', '$rootScope', 'PosUser', function ($scope, $rootScope, PosUser) {
        $scope.curr = 'scheduling';
        //获取当前收银员头像
        var user_head = $api.getStorage('user_head') ? $api.getStorage('user_head') : {};
        if (typeof $rootScope.user === "undefined" || typeof user_head[$rootScope.user.UserCode] === "undefined") {
            $scope.user_img = "./image/head_img_default.png";
        } else {
            $scope.user_img = user_head[$rootScope.user.UserCode];
        }
        $scope.schedule_options = [
            {
                id: 0,
                name: $rootScope.language.TIP_MYACCOUNT_SHOP_SCHEDULE1
            },
            {
                id: 1,
                name: $rootScope.language.TIP_MYACCOUNT_SHOP_SCHEDULE2
            },
            {
                id: 2,
                name: $rootScope.language.TIP_MYACCOUNT_SHOP_SCHEDULE3
            },
            {
                id: 3,
                name: $rootScope.language.TIP_MYACCOUNT_SHOP_SCHEDULE4
            }
        ];
        $scope.schedule_repeat = [];
        PosUser.get_user_by_shopCode(20).then(function (res) {
            res.forEach(function (i) {
                $scope.schedule_repeat.push({
                    name: i.UserName
                })
            });
        });
        $scope.options_value = 0;
    }];
MyAccountCtrl.$injector = ['$scope', '$rootScope'];
PersonalInfoCtrl.$injector = ['$scope', '$rootScope', '$location', 'UserLoginCheck', 'UserSetPicture', 'PosUser'];
ChangePassCtrl.$injector = ['$scope', '$rootScope', '$location', 'UserLoginCheck', 'UserSetPicture', 'PosUser', '$window'];
AttendUploadCtrl.$injector = ['$rootScope', '$scope', '$route', 'Upload', 'ShopAttend', 'PosUser'];
PerformanceSelectCtrl.$injector = ['$scope', '$rootScope', '$location', 'PeFormanceSelect', 'ngDialog', 'PosUser', 'timeStamp2String', '$sce', '$compile'];
ScheduleCtrl.$injector = ['$scope', '$rootScope', 'PosUser'];